#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include "times.h"

static struct option opt[] = {
    // now time after day.hour.minute.second
    {"day",    1, 0, 'd'}, 
    {"hour",   1, 0, 'h'}, 
    {"minute", 1, 0, 'm'}, 
    {"second", 1, 0, 's'},
    // specified time
    {"time",   1, 0, 't'}, 
    {"help",   0, 0, 'H'},
    {0,0,0,0}
};

void usage()
{
    printf("arguments specification:\n");
    printf(" -t specified year month day hour minute second.\n");
    printf("    bytes year4 month2 day2 hour2 minute2 second2\n");
    printf("    example format: 20001231235959\n");
    printf(" -d set redress days.\n");
    printf("    example : -d <number|-number>\n");
    printf(" -h set redress hours.\n");
    printf(" -m set redress minutes.\n");
    printf(" -s set redress seconds.\n");
    printf(" -h, -m, -s Using methods like the -d\n");
}

int main(int argc, char **argv)
{
    time_t t, target;
    char buf1[100], buf2[100];
    char c;

    long long seconds = 0;
    int  a;
    int specified = 0;
    char times[100], ctimes[50];

    int have_arg = 0;

    opterr = 1;
    while ((c = getopt_long(argc,argv,"d:h:m:s:t:H", opt, 0)) != -1) {
        switch(c) {
            case 'd':
                have_arg++;
                printf("day       %s\n", optarg);
                a = atoi(optarg);
                seconds += a * 60 * 60 * 24;
                break;
            case 'h':
                have_arg++;
                printf("hour      %s\n", optarg);
                a = atoi(optarg);
                seconds += a * 60 * 60;
                break;
            case 'm':
                have_arg++;
                printf("minute    %s\n", optarg);
                a = atoi(optarg);
                seconds += a * 60;
                break;
            case 's':
                have_arg++;
                printf("second    %s\n", optarg);
                a = atoi(optarg);
                seconds += a;
                break;
            case 't':
                have_arg++;
                specified = 1;
                printf("specified time %s\n", optarg);
                snprintf(times, sizeof(times), "%s", optarg);
                break;
            case 'H':
                usage();
                return 0;
                break;
            default:
                usage();
                exit(1);
                break;
        }
    }
    
    t = time(0);
    target = t;

    if (specified) {
        if (maketime(times, &target) < 0) {
            printf("%s: -t optarg format error, valid like \"20120825070855\"\n", argv[0]);
            return -1;
        }
    }

    times_friendly(t, buf1, sizeof(buf1));
    times_compact (t, buf2, sizeof(buf2));
    snprintf(ctimes, sizeof(ctimes), "%s", ctime(&t));
    if (strrchr(ctimes, '\n')) *strrchr(ctimes, '\n') = 0;
    printf("\e[33m%s\e[m \e[31m| \e[m", ctimes);
    printf("%s \e[31m==> \e\[m%s \e[31m==>\e\[m %-10ld \e[32m(Now)\e\[m\n", buf1, buf2, t);

    if (have_arg == 0) return 0;

    target += seconds;
    times_friendly(target, buf1, sizeof(buf1));
    times_compact (target, buf2, sizeof(buf2));
    snprintf(ctimes, sizeof(ctimes), "%s", ctime(&target));
    if (strrchr(ctimes, '\n')) *strrchr(ctimes, '\n') = 0;
    printf("\e[33m%s\e[m \e[31m| \e[m", ctimes);
    printf("%s \e[31m==> \e\[m%s \e[31m==>\e\[m %-10ld \e[31m(Target)\e[m\n", buf1, buf2, target);
//    // Show unix base time.
//    time_t tmp = 0;
//    times_friendly(tmp, buf1, sizeof(buf1));
//    times_compact (tmp, buf2, sizeof(buf2));
//    snprintf(ctimes, sizeof(ctimes), "%s", ctime(&tmp));
//    if (strrchr(ctimes, '\n')) *strrchr(ctimes, '\n') = 0;
//    printf("\e[33m%s\e[m ", ctimes);
//    printf("\e[33m%s\e[m \e[31m| \e[m", ctimes);
//    printf("%s \e[31m==> \e\[m%s \e[31m==>\e\[m %-10ld \e[32m(UNIX base)\e\[m\n", buf1, buf2, tmp);

//    t = time(0);
//    fmt_time(t, buf1, sizeof(buf1), times_compact); printf("[%s]\n", buf1);
//    fmt_time(t, buf1, sizeof(buf1), times_friendly); printf("[%s]\n", buf1);
//    fmt_time(t, buf1, sizeof(buf1), times_friendly_week); printf("[%s]\n", buf1);
//    fmt_time(t, buf1, sizeof(buf1), times_friendly_english); printf("[%s]\n", buf1);

    return 0;
}

